// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/dwio/common/data_buffer_holder.h>
#include <pollux/common/testutil/test_value.h>

using kumo::pollux::common::testutil::TestValue;

namespace kumo::pollux::dwio::common {
    void DataBufferHolder::take(const std::vector<melon::StringPiece> &buffers) {
        // compute size
        uint64_t totalSize = 0;
        for (auto &buf: buffers) {
            totalSize += buf.size();
        }
        if (totalSize == 0) {
            return;
        }

        TestValue::adjust(
            "kumo::pollux::dwio::common::DataBufferHolder::take", pool_);

        dwio::common::DataBuffer<char> buf(*pool_, totalSize);
        auto *data = buf.data();
        for (auto &buffer: buffers) {
            const auto size = buffer.size();
            ::memcpy(data, buffer.begin(), size);
            data += size;
        }
        // If possibly, write content of the data to output immediately. Otherwise,
        // make a copy and add it to buffer list
        if (sink_ != nullptr) {
            sink_->write(std::move(buf));
        } else {
            buffers_.push_back(std::move(buf));
        }
        size_ += totalSize;
    }

    bool DataBufferHolder::tryResize(
        dwio::common::DataBuffer<char> &buffer,
        uint64_t headerSize,
        uint64_t increment) const {
        auto size = buffer.size();
        // Makes sure size is at least header size
        if (MELON_LIKELY(size >= headerSize)) {
            size -= headerSize;
        } else {
            POLLUX_CHECK_EQ(size, 0);
        }

        POLLUX_CHECK_LE(size, maxSize_);
        // If already at max size, return.
        if (size == maxSize_) {
            return false;
        }

        // make sure size is at least same as initial size
        auto targetSize = size + increment;
        size = std::max(initialSize_, size);
        while (size < targetSize) {
            size = static_cast<uint64_t>(size * growRatio_);
            if (size >= maxSize_) {
                size = maxSize_;
                break;
            }
        }

        // make sure size is at least same as capacity
        size = std::max(size + headerSize, buffer.capacity());
        buffer.resize(size);
        return true;
    }
} // namespace kumo::pollux::dwio::common
